package com.le.tester.imooc.masterJUC.lock.deadlock;

import org.springframework.aop.scope.ScopedProxyUtils;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 描述：使用tryLock来避免死锁
 */
public class TryLockDeadLock implements Runnable {

    public int flag = 1;

    //两把可重入的锁
    public static Lock lock1 = new ReentrantLock();
    public static Lock lock2 = new ReentrantLock();

    @Override
    public void run() {
        try {
            for (int i = 0; i < 100; i++) {
                if (flag == 1) {
                    if (lock1.tryLock(800, TimeUnit.MICROSECONDS)) {
                        //获取到了第一把锁，继续获取到第二把锁
                        Thread.sleep(new Random(47).nextInt(1000));
                        if (lock2.tryLock(800, TimeUnit.MICROSECONDS)) {
                            System.out.println("线程:" + Thread.currentThread().getName() + "获取到了两把锁");
                            //开始释放锁
                            lock2.unlock();
                            lock1.unlock();
                            //跳出循环
                            break;
                        } else {
                            System.out.println("线程:" + Thread.currentThread().getName() + "获取锁2失败");
                            //应该释放第一把锁
                            lock1.unlock();
                            Thread.sleep(new Random(47).nextInt(1000));

                        }

                    } else {
                        System.out.println("线程:" + Thread.currentThread().getName() + "获取锁1失败");
                    }
                }

                if (flag == 0) {
                    if (lock2.tryLock(3000, TimeUnit.MICROSECONDS)) {
                        //获取到了第一把锁，继续获取到第二把锁
                        Thread.sleep(new Random(47).nextInt(1000));
                        if (lock1.tryLock(3000, TimeUnit.MICROSECONDS)) {
                            System.out.println("线程:" + Thread.currentThread().getName() + "获取到了两把锁");
                            //开始释放锁
                            lock1.unlock();
                            lock2.unlock();
                            //跳出循环
                            break;
                        } else {
                            System.out.println("线程:" + Thread.currentThread().getName() + "获取锁1失败");
                            //应该释放第一把锁
                            lock2.unlock();
                            Thread.sleep(new Random(47).nextInt(1000));

                        }

                    } else {
                        System.out.println("线程:" + Thread.currentThread().getName() + "获取锁2失败");
                    }

                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static void main(String[] args) {
        TryLockDeadLock tryLockDeadLock1 = new TryLockDeadLock();
        TryLockDeadLock tryLockDeadLock2 = new TryLockDeadLock();
        tryLockDeadLock1.flag = 1;
        tryLockDeadLock2.flag = 0;
        new Thread(tryLockDeadLock1).start();
        new Thread(tryLockDeadLock2).start();
    }
}
